package com.yanqu.road.dao.impl.activity.horserun.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.horserun.cross.CrossHorseRunZoneRoundRankData;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossHorseRunZoneRoundRankDataDaoImpl extends TempDataObjectDao<CrossHorseRunZoneRoundRankData> {
    public static String getCrossHorseRunZoneRoundRankDataSql = "select * from `t_c_horse_run_user_join_rank` where `activityId`=? and `groupId`=?";
    public List<CrossHorseRunZoneRoundRankData> getCrossHorseRunZoneRoundRankData(int activityId,int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossHorseRunZoneRoundRankData> rankDataList = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossHorseRunZoneRoundRankDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                rankDataList.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return rankDataList;
    }

    public static String updateCrossHorseRunZoneRoundRankDataSql = "update `t_c_horse_run_user_join_rank` set zone=?,rank=?,lockAttr=?,historyEarn=?,finishTime=? where activityId=? and groupId=? and roundId=? and userId=?";
    @Override
    protected boolean updateData(CrossHorseRunZoneRoundRankData crossHorseRunZoneRoundRankData) {
        crossHorseRunZoneRoundRankData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getZone()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunZoneRoundRankData.getLockAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHorseRunZoneRoundRankData.getHistoryEarn().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHorseRunZoneRoundRankData.getFinishTime().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getRoundId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunZoneRoundRankData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossHorseRunZoneRoundRankDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossHorseRunZoneRoundRankDataSql = "insert into `t_c_horse_run_user_join_rank`(activityId,groupId,roundId,zone,userId,rank,lockAttr,historyEarn,finishTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossHorseRunZoneRoundRankData crossHorseRunZoneRoundRankData) {
        crossHorseRunZoneRoundRankData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getRoundId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getZone()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunZoneRoundRankData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunZoneRoundRankData.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunZoneRoundRankData.getLockAttr())));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHorseRunZoneRoundRankData.getHistoryEarn().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHorseRunZoneRoundRankData.getFinishTime().toString()));
        if (getDb().executeNoneQuery(insertCrossHorseRunZoneRoundRankDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossHorseRunZoneRoundRankData parseData(ResultSet rs) throws SQLException {
        CrossHorseRunZoneRoundRankData crossHorseRunZoneRoundRankData = new CrossHorseRunZoneRoundRankData();
        crossHorseRunZoneRoundRankData.setActivityId(rs.getInt("activityId"));
        crossHorseRunZoneRoundRankData.setGroupId(rs.getInt("groupId"));
        crossHorseRunZoneRoundRankData.setRoundId(rs.getInt("roundId"));
        crossHorseRunZoneRoundRankData.setZone(rs.getInt("zone"));
        crossHorseRunZoneRoundRankData.setUserId(rs.getLong("userId"));
        crossHorseRunZoneRoundRankData.setRank(rs.getInt("rank"));
        crossHorseRunZoneRoundRankData.setLockAttr(JSON.parseObject(rs.getString("lockAttr"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        crossHorseRunZoneRoundRankData.setHistoryEarn(new BigInteger(rs.getString("historyEarn")));
        crossHorseRunZoneRoundRankData.setFinishTime(new BigDecimal(rs.getString("finishTime")));

        return crossHorseRunZoneRoundRankData;
    }

}
